﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;

namespace DAO
{
   public class PhongDAO
    {
       public List<PhongDTO> DanhSachPhong()
       {
           List<PhongDTO> kq = new List<PhongDTO>();
           DataProvider dp = new DataProvider();
           string strsql = "select*from Phong";
           dp.Connect();
           SqlDataReader dr = dp.ExecuteQuery(strsql);
           if(dr.HasRows)
           {
               while (dr.Read())
               {
                   PhongDTO p = new PhongDTO();
                   p.P_ID = int.Parse(dr["P_ID"].ToString());
                   p.MaTrangThaiPhong = int.Parse(dr["MaTrangThaiPhong"].ToString());
                   p.MaPhong = dr["MaPhong"].ToString();
                   p.MaLoaiPhong = int.Parse(dr["MaLoaiPhong"].ToString());
                   kq.Add(p);
               }
           }
           return kq;
       }
       public PhongDTO LayPhongByID(int p_ID)
       {
           PhongDTO kq = new PhongDTO();
           DataProvider dp = new DataProvider();
           string strsql = "select*from phong where P_ID=" + p_ID;
           dp.Connect();
           SqlDataReader dr= dp.ExecuteQuery(strsql);
           dr.Read();
           kq.P_ID = int.Parse(dr["P_ID"].ToString());
           kq.MaTrangThaiPhong = int.Parse(dr["MaTrangThaiPhong"].ToString());
           kq.MaPhong = dr["MaPhong"].ToString();
           kq.MaLoaiPhong = int.Parse(dr["MaLoaiPhong"].ToString());
           dp.Disconnect();
           return kq;

       }
       public PhongDTO LayPhongByMaPhong(string maPhong)
       {
           PhongDTO kq = new PhongDTO();
           DataProvider dp = new DataProvider();
           string strsql = "select*from phong where MaPhong='"+maPhong+"'";
           dp.Connect();
           SqlDataReader dr = dp.ExecuteQuery(strsql);
           dr.Read();
           kq.P_ID = int.Parse(dr["P_ID"].ToString());
           kq.MaTrangThaiPhong = int.Parse(dr["MaTrangThaiPhong"].ToString());
           kq.MaPhong = dr["MaPhong"].ToString();
           kq.MaLoaiPhong = int.Parse(dr["MaLoaiPhong"].ToString());
           dp.Disconnect();
           return kq;
       }
       public Boolean ThemPhong(PhongDTO p)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           dp.Connect();
           string strsql = "insert into Phong(MaTrangThaiPhong,MaPhong,MaLoaiPhong)";
           strsql += "values(" + p.MaTrangThaiPhong + ",'" + p.MaPhong + "'," + p.MaLoaiPhong + ")";
           int i = dp.ExecuteNonQuery(strsql);
           if (i != 0)
               kq = true;
           dp.Disconnect();           
           return kq;
       }
       public Boolean XoaPhongByID(int p_ID)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           dp.Connect();
           string strsql = "delete from where Phong where P_ID="+p_ID;
           int i = dp.ExecuteNonQuery(strsql);
           if (i != 0)
               kq = true;
           dp.Disconnect();
           return kq;
       }
       public Boolean CapNhatPhong(PhongDTO p)
       {
           Boolean kq = false;
           DataProvider dp = new DataProvider();
           dp.Connect();
           string strsql = "update Phong set MaTrangThaiPhong="+p.MaTrangThaiPhong+" and MaPhong='"+p.MaPhong+"' and MaLoaiPhong="+p.MaLoaiPhong+" where P_ID="+p.P_ID;
           int i = dp.ExecuteNonQuery(strsql);
           if (i != 0)
               kq = true;
           dp.Disconnect();
           return kq;
       }

    }
}
